package ssi.actions;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.ArrayList;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

import ssi.beans.Search;

import ssi.tools.Connector;

public class SearchAction  extends Action {
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		request.setCharacterEncoding("UTF-8");
		String target = null;
		int licznik = 0;
		String selectString = null;
		ArrayList<String> tabela = new ArrayList<String>(licznik * 5);
		Search searching=(Search)form;
		String type=searching.getType();
		String search=searching.getSearch();
		ActionErrors actionErrors = new ActionErrors();
		System.out.println(type + search);
		String arg = "";
		if(!(search.equals("") || search==null)){
			if(type.equals("Użytkownik")){
				selectString = "SELECT * FROM users where name LIKE '%" +search + "%' or surname LIKE '%"+search + "%' or email LIKE '%"+search + "%' or address LIKE '%"+search + "%' or pesel LIKE '%"+search+ "%' or id LIKE '%"+search + "%' or type LIKE '%"+search+"%'";
			}else if(type.equals("Zwierzę")){
				selectString = "SELECT * FROM animals where id LIKE '%" +search + "%' or breed LIKE '%"+search + "%' or gender LIKE '%"+search + "%' or name LIKE '%"+search + "%'";
				System.out.println(selectString);
			}else if(type.equals("Ogłoszenie")){
				selectString = "SELECT * FROM ad where id LIKE '%" +search + "%' or title LIKE '%"+search + "%' or ad LIKE '%"+search + "%'";
			}else{
				target = "failure";
				actionErrors.add("error",
						new ActionMessage("error.emptyType"));
				arg="Prosze wpisac dane do wyszukania";
				request.getSession().setAttribute("searchList",arg);
				return mapping.findForward(target);
			}
		}else{
			target = "failure";
			actionErrors.add("error",
					new ActionMessage("error.emptyField"));
			arg="Prosze wpisac dane do wyszukania";
			request.getSession().setAttribute("searchList",arg);
			return mapping.findForward(target);
		}

		try {
			Connection connection = new Connector().getConnection();
    		Statement statement = connection.createStatement();
			ResultSet result = statement.executeQuery(selectString);
		
				while (result.next()) {
					if(type.equals("Użytkownik")){
						tabela.add("<a href=\"index.jsp?page=userProfile&id="+result.getString("id")+"\">" + result.getString("name") + " " + result.getString("surname") + "</a><br>");					
					}else if(type.equals("Zwierzę")){
		                tabela.add("<a href=\"index.jsp?page=animalProfile&id="+result.getString("id")+"\">" + result.getString("breed") +" " + result.getString("name") + "</a><br>");
					}else if(type.equals("Ogłoszenie")){
						tabela.add("</h3><img src='images/upload/"+result.getString("images")+"' class='image_frame_430' /><p class='date'>"+result.getString("date")+"</p><p><em><div class='comment more'>"+result.getString("ad")+"</div><br><br>Jestes zainteresowany?</em> <a href='index.jsp?page=animalProfile&id="+result.getString("animalId")+"'>Przejdz do profilu zwierzaka!</a></p><a href='index.jsp?page=showAds' class='more2'>Wiecej ogloszen</a> <br class='cleaner' />");
					}
					licznik++;
				}
			
			
			int z = 0;
			for (int a = 0; a < tabela.size(); a++) {
				arg += tabela.get(a) + " ";
				z++;
				if (z % 5 == 0) {
					arg += "<br>";
				}
			}
		
			if (tabela.isEmpty()) {
				arg = "Brak wyników dla podanych kryteriów.";
			}
			target="success";
		
			
			request.getSession().setAttribute("searchList",arg);
		
			result.close();
			connection.close();
			
			
	}catch(Exception e){
		target="failure";
		e.printStackTrace();
		actionErrors.add("error",
				new ActionMessage("error.emptyField"));
	}
		
		if (actionErrors.size() != 0) {
			extracted(request, actionErrors);
			return new ActionForward(mapping.getInput());
		}
		return mapping.findForward(target);
		
	}
	
	private void extracted(HttpServletRequest request, ActionErrors actionErrors) {
		saveErrors(request, actionErrors);
	}

}
